#include <iostream>
#include <cmath>
#include <windows.h>  // 添加Windows头文件以支持控制台编码设置

using namespace std;

// 计算两组对应序号数字的平均值
bool calculateLineGrayscale(int& result1, int& result2, int& result3, int& result4, int& result5, int& result6) {
    cout << "请输入第一组六个数字：";
    int line1[6];
    for (int i = 0; i < 6; i++) {
        if (!(cin >> line1[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    cout << "请输入第二组六个数字：";
    int line2[6];
    for (int i = 0; i < 6; i++) {
        if (!(cin >> line2[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    // 计算每组对应序号数字的平均值（相加除二，取整）
    result1 = (line1[0] + line2[0]) / 2;
    result2 = (line1[1] + line2[1]) / 2;
    result3 = (line1[2] + line2[2]) / 2;
    result4 = (line1[3] + line2[3]) / 2;
    result5 = (line1[4] + line2[4]) / 2;
    result6 = (line1[5] + line2[5]) / 2;
    
    return true;
}

// 计算色块地灰值
bool calculateBlockGrayscale(int& whiteResult, int& yellowResult, int& finalResult) {
    cout << "请输入白色色块左侧：";
    int whiteLeft;
    if (!(cin >> whiteLeft)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入白色色块右侧：";
    int whiteRight;
    if (!(cin >> whiteRight)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入黄色色块左侧：";
    int yellowLeft;
    if (!(cin >> yellowLeft)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入黄色色块右侧：";
    int yellowRight;
    if (!(cin >> yellowRight)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    // 计算白色色块的平均值
    whiteResult = (whiteRight + whiteLeft) / 2;
    // 计算黄色色块的平均值
    yellowResult = (yellowRight + yellowLeft) / 2;
    // 计算最终结果（两个平均值相加再除以二）
    finalResult = (whiteResult + yellowResult) / 2;
    
    return true;
}

// 计算分球地灰值
bool calculateBallGrayscale(int& mBallResult, int& mWYResult) {
    cout << "请输入无球时分球地灰值：";
    int noBallValue;
    if (!(cin >> noBallValue)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入有黄球时分球地灰值(分别取三颗球测量)：";
    int yellowBalls[3];
    for (int i = 0; i < 3; i++) {
        if (!(cin >> yellowBalls[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    int yellowResult = (noBallValue + yellowBalls[0] + yellowBalls[1] + yellowBalls[2]) / 4;
    
    cout << "请输入有白球时分球地灰值(分别取三颗球测量)：";
    int whiteBalls[3];
    for (int i = 0; i < 3; i++) {
        if (!(cin >> whiteBalls[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    int whiteResult = (noBallValue + whiteBalls[0] + whiteBalls[1] + whiteBalls[2]) / 4;
    
    mBallResult = (yellowResult + noBallValue) / 2;
    mWYResult = (yellowResult + whiteResult) / 2;
    
    return true;
}

// 计算LR区域灰度值
bool calculateLRGrayscale(int& lResult, int& rResult) {
    cout << "请输入L无球时分球地灰值：";
    int lNoBall;
    if (!(cin >> lNoBall)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入L有白球时分球地灰值(分别取三颗球测量)：";
    int lBalls[3];
    for (int i = 0; i < 3; i++) {
        if (!(cin >> lBalls[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    lResult = (lNoBall + lBalls[0] + lBalls[1] + lBalls[2]) / 4;
    
    cout << "请输入R无球时分球地灰值：";
    int rNoBall;
    if (!(cin >> rNoBall)) {
        cout << "输入错误，请输入有效数字！" << endl;
        cin.clear();
        cin.ignore(1000, '\n');
        return false;
    }
    
    cout << "请输入R有白黄球时分球地灰值(分别取三颗球测量)：";
    int rBalls[3];
    for (int i = 0; i < 3; i++) {
        if (!(cin >> rBalls[i])) {
            cout << "输入错误，请输入有效数字！" << endl;
            cin.clear();
            cin.ignore(1000, '\n');
            return false;
        }
    }
    
    rResult = (rNoBall + rBalls[0] + rBalls[1] + rBalls[2]) / 4;
    
    return true;
}

int main(){
    // 设置控制台输出编码为UTF-8，确保中文正常显示
    SetConsoleOutputCP(CP_UTF8);
    
    // 计算巡线地灰
    int lineResult1, lineResult2, lineResult3, lineResult4, lineResult5, lineResult6;
    if (calculateLineGrayscale(lineResult1, lineResult2, lineResult3, lineResult4, lineResult5, lineResult6)) {
        cout << "巡线地灰计算结果：" << lineResult1 << " " << lineResult2 << " " << lineResult3 << " " 
             << lineResult4 << " " << lineResult5 << " " << lineResult6 << endl;
    } else {
        cout << "巡线地灰计算失败！" << endl;
        return 1;
    }
    
    // 计算色块地灰
    int whiteBlockResult, yellowBlockResult, finalBlockResult;
    if (calculateBlockGrayscale(whiteBlockResult, yellowBlockResult, finalBlockResult)) {
        cout << "白色色块的平均值：" << whiteBlockResult << endl;
        cout << "黄色色块的平均值：" << yellowBlockResult << endl;
        cout << "色块地灰最终计算结果：" << finalBlockResult << endl;
    } else {
        cout << "色块地灰计算失败！" << endl;
        return 1;
    }
    
    // 计算分球地灰值
    int mBallResult, mWYResult;
    if (calculateBallGrayscale(mBallResult, mWYResult)) {
        cout << "mBall的灰度值为：" << mBallResult << endl;
        cout << "mWY的灰度值为：" << mWYResult << endl;
    } else {
        cout << "分球地灰值计算失败！" << endl;
        return 1;
    }
    
    // 计算LR区域灰度值
    int lResult, rResult;
    if (calculateLRGrayscale(lResult, rResult)) {
        cout << "L的灰度值为：" << lResult << endl;
        cout << "R的灰度值为：" << rResult << endl;
    } else {
        cout << "LR区域灰度值计算失败！" << endl;
        return 1;
    }
    
    cout << "所有计算完成，请按任意键继续..." << endl;
    cin.ignore();  // 忽略前面的换行符
    cin.get();     // 等待用户按键
    
    return 0;
}